<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/jquery-3.6.0.min.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        a {
            text-decoration: none;
            color: inherit;
        }

        li {
            list-style: none;
        }

        img {
            vertical-align: top;
        }

        /* 商品展示 */
        #showGoods {
            width: 1226px;
            margin: 0 auto;
            margin-top: 50px;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
        }

        #showGoods li {
            width: 18%;
            box-sizing: border-box;
            padding: 10px;
            font-size: 13px;
            line-height: 20px;
        }

        #showGoods li:hover {
            box-shadow: 0 0 1px 2px rgba(0, 0, 0, 0.2);
        }

        #showGoods li img {
            width: 70%;
            display: block;
            margin: 20px auto;
        }

        #showGoods li .price {
            color: red;
        }

        #showGoods li .name {
            /* 多行文本溢出隐藏 */
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        #searchBox {
            width: 1226px;
            height: 32px;
            margin: 50px auto;
            display: flex;
            font-size: 13px;
        }

        #searchBox .searchBar {
            width: 300px;
            height: 32px;
            border: 1px solid #333;
            display: flex;
        }

        #searchBox .searchBar .searchCon {
            outline: none;
            border: 0;
            width: 268px;
            height: 32px;
        }

        #searchBox .searchBar .searchBtn {
            flex: 1;
            background-color: grey;
        }

        .orderColBox,
        .orderTypeBox {
            margin-left: 20px;
            line-height: 32px;
            padding: 0 10px;
            border: 1px dashed #333;
        }

        .orderColBox .active {
            color: pink;
        }

        .orderTypeBox .active {
            color: pink;
        }

        .showPageBox {
            margin-left: 20px;
            line-height: 32px;
        }

        .showPageBox select {
            height: 32px;
            outline: none;
            vertical-align: middle;
        }
    </style>
</head>

<body>
    <div id="searchBox">
        <div class="searchBar">
            <input type="text" class="searchCon">
            <a href="javascript:;" class="searchBtn"></a>
        </div>
        <div class="orderColBox">
            <span class="orderCol active" data-col="id">默认</span>
            <span class="orderCol" data-col="goodsName">名称</span>
            <span class="orderCol" data-col="goodsPrice">价格</span>
        </div>
        <div class="orderTypeBox">
            <span class="orderType active" data-type="asc">升序</span>
            <span class="orderType" data-type="desc">降序</span>
        </div>
        <div class="showPageBox">
            <select class="showPage">
                <option value="5">每页显示5条</option>
                <option value="10">每页显示10条</option>
                <option value="15">每页显示15条</option>
                <option value="20">每页显示20条</option>
            </select>
        </div>
    </div>
    <ul id="showGoods">
        <!-- <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/195641/2/30490/89452/63745247Efb2a8608/46028f28c82c3faf.jpg"
                    alt="">
                <div class="price">6499.00</div>
                <div class="name">华为（HUAWEI） 笔记本电脑MateBook D16 16英寸高性能轻薄本商务办公学生手提超极本 银丨十二代i5-12500H 16G 512G</div>
            </a>
        </li> -->
    </ul>
</body>
<script>

    // 完成商品 搜索 排序 分页 -> 传五个参数(搜索的关键词 按哪个数据排序  排序方式  显示第几页  每页显示多少条)  => 五项中有一项变动  -> 重新发送请求
    var wd = ""; //全局变量  记录搜索的关键词   "" => 默认查询所有数据
    var col = "id"; // 全局变量  按哪个数据排序  默认按id排序
    var type = "asc"; //  全局变量  排序方式   默认升序
    var page = 1;   // 全局变量  记录页码(默认显示第一页)
    var size = 5;   // 全局变量  记录每页显示几条数据


    loadGoods();  // 页面加载时请求所有商品  


    $(".searchBtn").click(function () {
        wd = $(".searchCon").val();
        console.log(wd);

        loadGoods();

    })



    // tab导航切换

    $(".orderCol").click(function () {
        $(this).addClass("active").siblings().removeClass("active");

        // col = this.dataset.col;
        col = $(this).prop("dataset").col;
        console.log(col);

        loadGoods();

    })

    $(".orderType").click(function () {
        $(this).addClass("active").siblings().removeClass("active");

        type = this.dataset.type;
        console.log(type);

        loadGoods();
    })


    $(".showPage").change(function () {
        size = $(this).val();

        loadGoods();
    })




    function loadGoods() {

        // 关键词改变  => 发送请求  

        var xhr = new XMLHttpRequest();

        // ajax get传参 => 将参数数据队列拼接到请求地址之后,用?分隔

        // xhr.open("get", "http://43.138.81.225/demo/php/searchGoodsOrderLimit.php?wd=" + wd + "&col=" + col + "&type=" + type + "&page=" + page + "&size=" + size, true);
        xhr.open("get", `http://43.138.81.225/demo/php/searchGoodsOrderLimit.php?wd=${wd}&col=${col}&type=${type}&page=${page}&size=${size}`, true);

        xhr.send();

        xhr.onreadystatechange = function () {
            // 2 3 4
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText;
                // console.log("请求成功", result);

                result = JSON.parse(result);
                console.log(result);

                // code后端接口返回的信息  -> 请求成功 失败 
                var { status, message, list } = result;

                if (status) {
                    var html = "";

                    list.forEach(({ goodsName, goodsImg, goodsPrice }) => {
                        html += `<li>
                        <a href="">
                            <img src="${goodsImg}"
                                alt="">
                            <div class="price">${goodsPrice}</div>
                            <div class="name">${goodsName}</div>
                        </a>
                    </li>`;
                    });

                    showGoods.innerHTML = html;
                }

            }
        }

    }




</script>

</html>